var rndtoday = new Date(); 
var rndseed = rndtoday.getTime(); 

function rnd() { 
	rndseed = (rndseed*9301+49297) % 233280; 
	return rndseed/(233280.0); 
}; 

function rand(number) { 
	return Math.floor(rnd()*number); 
};

function $(id)
{
	return document.getElementById(id);
}

function loadFile(title)
{
	try {
		netscape.security.PrivilegeManager.enablePrivilege("UniversalXPConnect");
	} catch (e) {
		alert("Permission to save file was denied.");
	}
	
	const nsIFilePicker = Components.interfaces.nsIFilePicker;
	
	var fp = Components.classes["@mozilla.org/filepicker;1"]
			   .createInstance(nsIFilePicker);
	fp.init(window, title, nsIFilePicker.modeOpen);
	fp.appendFilters(nsIFilePicker.filterAll | nsIFilePicker.filterText);
	
	var rv = fp.show();
	if (rv == nsIFilePicker.returnOK)
	{
		var file = fp.file;
		// Get the path as string. Note that you usually won't 
		// need to work with the string paths.
		var path = fp.file.path;
		// work with returned nsILocalFile...
		return path;
	} else {
		return null;
	}
}

function saveFile(string, savefile)
{
	try {
		netscape.security.PrivilegeManager.enablePrivilege("UniversalXPConnect");
	} catch (e) {
		alert("Permission to save file was denied.");
	}
	var outputStream = getOutputStream(savefile);
	if(!outputStream) return;
	/* Open flags 
	#define PR_RDONLY       0x01
	#define PR_WRONLY       0x02
	#define PR_RDWR         0x04
	#define PR_CREATE_FILE  0x08
	#define PR_APPEND      0x10
	#define PR_TRUNCATE     0x20
	#define PR_SYNC         0x40
	#define PR_EXCL         0x80
	*/
	/*
	** File modes ....
	**
	** CAVEAT: 'mode' is currently only applicable on UNIX platforms.
	** The 'mode' argument may be ignored by PR_Open on other platforms.
	**
	**   00400   Read by owner.
	**   00200   Write by owner.
	**   00100   Execute (search if a directory) by owner.
	**   00040   Read by group.
	**   00020   Write by group.
	**   00010   Execute by group.
	**   00004   Read by others.
	**   00002   Write by others
	**   00001   Execute by others.
	**
	*/
	var result = outputStream.write( string, string.length );
	outputStream.close();
}
function saveUTF8File(string, savefile)
{
	try {
		netscape.security.PrivilegeManager.enablePrivilege("UniversalXPConnect");
	} catch (e) {
		alert("Permission to save file was denied.");
	}
	
	var charset = "UTF-8";
	var outputStream = getOutputStream(savefile);
	if(!outputStream) return;
	var os = Components.classes["@mozilla.org/intl/converter-output-stream;1"]
                   .createInstance(Components.interfaces.nsIConverterOutputStream);
	os.init(outputStream, charset, 0, 0x0000);
	os.writeString(string);
	os.close();
	outputStream.close();
}
function getOutputStream(savefile)
{
	try {
		netscape.security.PrivilegeManager.enablePrivilege("UniversalXPConnect");
	} catch (e) {
		alert("Permission to save file was denied.");
	}
	
	if(!savefile) return null;
	var file = Components.classes["@mozilla.org/file/local;1"]
		.createInstance(Components.interfaces.nsILocalFile);
	file.initWithPath( savefile );
	if ( file.exists() == false ) {
		file.create( Components.interfaces.nsIFile.NORMAL_FILE_TYPE, 420 );
	}
	var outputStream = Components.classes["@mozilla.org/network/file-output-stream;1"]
		.createInstance( Components.interfaces.nsIFileOutputStream );
	outputStream.init( file, 0x04 | 0x08 | 0x20, 420, 0 );
	return outputStream;
}
/*only for getting list item value*/
function get_item_id(elementId)
{
	var e = document.getElementById(elementId);
	var ret = -1;
	
	if (e == null) return ret;

	if (e.selectedItem != null) {
		ret = parseInt(e.selectedItem.value);
	} else if (e.getItemAtIndex(0) != null) {
		ret = parseInt(e.getItemAtIndex(0).value);
	}
	
	return ret;
}


function timeStr2Int(strTime)
{ /*xx:xx:xx.xxx to msec*/
	if (strTime == null) return 0;
	
	var h = parseInt(strTime.substring(0, 2), 10);
	var m = parseInt(strTime.substring(3, 5), 10);
	var s = parseInt(strTime.substring(6, 8), 10);
	var r = parseInt(strTime.substring(9, 12), 10);
//	alert( strTime + "->"+ h + " " + m + " " + s + " " + r);
	return (h * 3600 + m * 60 + s) * 1000 + r;
}

function pick_dir()
{
	try {
		netscape.security.PrivilegeManager.enablePrivilege("UniversalXPConnect");
	} catch (e) {
		alert("Permission to save file was denied.");
	}
	
	var nsIFilePicker = Components.interfaces.nsIFilePicker;
	var fp = Components.classes["@mozilla.org/filepicker;1"].createInstance(nsIFilePicker);
	fp.init(window, "Choose a folder", nsIFilePicker.modeGetFolder);
	
	var res = fp.show();
	if (res == nsIFilePicker.returnOK){
		var dir = fp.file.path;
		return dir;
	}
	
	return null;
}

function refreshSubElements(id)
{
	var e = document.getElementById(id);
	var sub;
	if (!e) return;
	for (e = e.firstChild; e; e = e.nextSibling) {
		for (sub = e.firstChild; sub; sub = sub.nextSibling) {
			switch (sub.nodeName) {
			case "menulist":
				sub = sub.selectedItem;
				found = true;
				break;
			default:
				continue;
			}
			break;
		}
		if (!sub) continue;
		if (sub.onclick)
			sub.onclick();
		else if (e.onchange)
			sub.onchange();
	}
}

function ask(text, caption)
{
			  try {
					netscape.security.PrivilegeManager.enablePrivilege("UniversalXPConnect");
				} catch (e) {
					alert("Permission to save file was denied.");
				}
        var promptService = Components.classes["@mozilla.org/embedcomp/prompt-service;1"]
                            .getService(Components.interfaces.nsIPromptService);
        var checkResult = {};
        var ret = promptService.confirmCheck(window, caption, text, null, checkResult);
        //return checkResult.value
        return ret;
        
}

function msgbox(text, caption)
{
        try {
					netscape.security.PrivilegeManager.enablePrivilege("UniversalXPConnect");
				} catch (e) {
					alert("Permission to save file was denied.");
				}
				
        var promptService = Components.classes["@mozilla.org/embedcomp/prompt-service;1"]
                            .getService(Components.interfaces.nsIPromptService);
        var ret = promptService.alert(window, caption, text);
}

function prompt(text, caption, defaultValue)
{
        try {
					netscape.security.PrivilegeManager.enablePrivilege("UniversalXPConnect");
				} catch (e) {
					alert("Permission to save file was denied.");
				}
				
        var promptService = Components.classes["@mozilla.org/embedcomp/prompt-service;1"]
                            .getService(Components.interfaces.nsIPromptService);
        var checkResult = {};
				var input = {value: defaultValue};
        var ret = promptService.prompt(window, caption, text, input, null, checkResult);
        return ret ? input.value : null;
}

function isChecked(id)
{
	var e = document.getElementById(id);
	return e && e.getAttribute("checked") == "checked";
}

function getValue(id)
{
	return document.getElementById(id).value;
}

function setValue(id, value)
{
	return document.getElementById(id).value = value;
}

function getValueInt(id)
{
	return parseInt(document.getElementById(id).value);
}

function getValueFloat(id)
{
	return parseFloat(document.getElementById(id).value);
}

function getIndex(id)
{
	return document.getElementById(id).selectedIndex;
}

function getFileTitle(filename)
{
        var i = filename.lastIndexOf('\\');
        var str;
        if (i > 0)
                str = filename.substr(i + 1);
        else
                str = filename;
        i = str.lastIndexOf('.');
        if (i > 0)
                str = str.substr(0, i);

	/*		
	i = str.indexOf(" - ");
	if (i > 0) str = str.substr(i + 3);
	i = str.indexOf("-");
	if (i > 0) str = str.substr(i + 1);
	*/

	for (var i = 0; i < str.length; i++) {
		switch (str.charAt(i)) {
		case "&":
			str = str.substr(0, i) + "&amp;" + str.substr(i + 1)
			i += 4;
			break;
		case "<":
			str = str.substr(0, i) + "&lt;" + str.substr(i + 1)
			i += 3;
			break;
		case ">":
			str = str.substr(0, i) + "&gt;" + str.substr(i + 1)
			i += 3;
			break;
		case "_":
			str = str.substr(0, i) + " " + str.substr(i + 1)
			i ++;
			break;
		}
			
	}

	return str;
}

function loadValue(id)
{
	var e = document.getElementById(id);
	if (e) {
		var v = e.getAttribute("data");
		if (v) e.value = v;
	}
}

function saveValue(id)
{
	var e = document.getElementById(id);
	if (e) e.setAttribute("data", e.value);
}

function loadSelectedIndex(id)
{
	var e = document.getElementById(id);
	var v= e.getAttribute("data");
	if (v) e.selectedIndex = v;
}

function clearListBox(id)
{
	var i;
	var listbox = document.getElementById(id);
	while (listbox.removeItemAt(0));
}

function getAttr(id, attr)
{
	var e = document.getElementById(id); 
	if (e) return e.getAttribute(attr);
	return null;
}

function setAttr(id, attr, val)
{
	return document.getElementById(id).setAttribute(attr, val);
}

function enable(id)
{
	document.getElementById(id).setAttribute("disabled", false);
}

function disable(id)
{
	document.getElementById(id).setAttribute("disabled", true);	
}

function dupAttr(id, attrFrom, attrTo)
{
	var e = document.getElementById(id);
	if (e) {
		var v = e.getAttribute(attrFrom);
		if (v) e.setAttribute(attrTo, v);
	}
}

function getAttrInt(id, attr)
{
	var e = document.getElementById(id); 
	if (e) return parseInt(e.getAttribute(attr));
	return null;
}

function getAttrFloat(id, attr)
{
	var e = document.getElementById(id); 
	if (e) return parseFloat(e.getAttribute(attr));
	return null;
}

function padNumber2(num)
{
	return (num < 10 ? "0" : "") + num;
}